package com.yy.micro.magic.data.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yy.micro.magic.data.entity.DynamicTriggerEntity;
import com.yy.micro.magic.data.model.vo.JpaObjectWithDynamicFieldsVo;
import com.yy.micro.magic.ext.domain.filter.DynamicTrigger;

import java.util.Collections;
import java.util.List;

public class DynamicTriggerVo extends JpaObjectWithDynamicFieldsVo {

    private Long id;

    private String planCode;

    private boolean disable;

    private String trigger;

    private List<String> patterns;

    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public String getPlanCode() {
        return planCode;
    }

    public void setPlanCode(final String planCode) {
        this.planCode = planCode;
    }

    public boolean isDisable() {
        return disable;
    }

    public void setDisable(final boolean disable) {
        this.disable = disable;
    }

    public String getTrigger() {
        return trigger;
    }

    public void setTrigger(final String trigger) {
        this.trigger = trigger;
    }

    public List<String> getPatterns() {
        return null==patterns? Collections.emptyList():patterns;
    }

    public void setPatterns(final List<String> patterns) {
        this.patterns = patterns;
    }

    @JsonIgnore
    public static DynamicTriggerVo create(){
        return new DynamicTriggerVo();
    }

    @JsonIgnore
    public DynamicTriggerVo from(DynamicTriggerEntity entity){
        super.from(entity);
        setDisable(entity.isDisable());
        setTrigger(entity.getTrigger());
        setId(entity.getId());
        setPlanCode(entity.getPlanCode());
        setPatterns(entity.getPatterns());
        return this;
    }

    @JsonIgnore
    public DynamicTriggerEntity to(){
        DynamicTriggerEntity entity = new DynamicTriggerEntity();
        super.to(entity);
        entity.setDisable(isDisable());
        entity.setTrigger(getTrigger());
        entity.setId(getId());
        entity.setPatterns(getPatterns());
        entity.setPlanCode(getPlanCode());
        return entity;
    }

    @JsonIgnore
    public DynamicTrigger toDomain(){
        DynamicTrigger obj = new DynamicTrigger();
        obj.setTrigger(getTrigger());
        obj.setPatterns(getPatterns().toArray(new String[0]));
        obj.setPlanCode(getPlanCode());
        return obj;
    }
}
